//
//  ContactVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/4.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "ContactVC.h"
#import "GroupMenberCell.h"
#import "SettingNormalCell.h"
#import "DepartDetailVC.h"
#import "GroupListVC.h"
#import "GroupMemberDetailVC.h"
#import "UIImageView+WebCache.h"
#import "CContactModel.h"
#import "CDepartmentModel.h"
#import "CDepartmentMgr.h"
#import "CXmppRequestMgr.h"
#import "CContactMgr.h"
#import "NSArray+PinYin.h"
#import "CAccountModel.h"
#import "CFolderMgr.h"
#import "CContactModel.h"
#import "MyInformationVC.h"
#import "CRecentlyContactMgr.h"
#import "CRecentlyContactModel.h"
#import "CGroupModel.h"
#import "CGroupMgr.h"
#import "CHChatRoomBase.h"
#import "IMSearchDisplayController.h"
#import "EFengDiscussController.h"
#import "EFengMIneContactController.h"

#define KSECTIONHEIGHT 35.0f
#define KNORMALCELLHEIGHT 60.0f

@interface ContactVC ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
{
    UITableView *_tableView;
    UISearchBar *_searchBar;
    UISearchDisplayController *_searchDisplayController;
    NSMutableArray *_searchAllFirstLetter;
    NSMutableArray *_searchSectionListArray;

}


@property (nonatomic, strong) NSMutableArray *contactList;

@property (nonatomic, strong) CContactModel *curContact;
@end

@implementation ContactVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSMutableArray *)contactList
{
    if (_contactList == nil) {
        _contactList = [NSMutableArray array];
    }
    return _contactList;
}

- (void)dealloc
{
    NSLog(@"contactVc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   
}
//UISearchDisplayDelegate
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"联系人"];
    
    _tableView = [[UITableView alloc] init];
//    _tableView.frame = self.view.frame;
    _tableView.frame = CGRectMake(0, 44,HARDWARE_SCREEN_WIDTH,HARDWARE_SCREEN_HEIGHT-49 - 64 - 44);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.sectionIndexBackgroundColor = ClearColor;
    _tableView.sectionIndexTrackingBackgroundColor = ClearColor;

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    _searchBar.backgroundImage = [UIImage imageNamed:@"BG-search"];
    //test 联系人搜索
    _searchBar.placeholder = @"搜索";
    _searchBar.delegate = self;
    // 添加 searchbar 到 headerview
    [self.view addSubview:_searchBar];
    
    _searchDisplayController = [[IMSearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    
    // searchResultsDataSource 就是 UITableViewDataSource
    _searchDisplayController.searchResultsDataSource = self;
    // searchResultsDelegate 就是 UITableViewDelegate
    _searchDisplayController.searchResultsDelegate = self;
    _searchDisplayController.delegate = self;
    _searchDisplayController.searchResultsTableView.sectionIndexColor = RGBCOLOR(0x64, 0x64, 0x64);
    _searchDisplayController.searchResultsTableView.sectionIndexBackgroundColor = ClearColor;
    _searchDisplayController.searchResultsTableView.sectionIndexTrackingBackgroundColor = ClearColor;

   self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
   
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(44, 170, 253)];
    } else {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(44, 170, 253)];
    }

    _tableView.frame = CGRectMake(0, 44,HARDWARE_SCREEN_WIDTH,HARDWARE_SCREEN_HEIGHT - 49 - 64 - 44);
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}
//- (void)addOftenContact
//{
//    [self.contactList removeAllObjects];
//    NSArray *array = [[[CRecentlyContactMgr alloc] init] loadAllRecentlyContacts];
//    NSMutableArray *arrayM = [NSMutableArray array];
//    CContactMgr *conMgr = [[CContactMgr alloc] init];
//    for (CRecentlyContactModel *model in array) {
//        CContactModel *contact = [conMgr loadNotDelContactWithUserId:model.userId];
//        if (contact) {
//            [arrayM addObject:contact];
//        }
//    }
//    
//    [self.contactList addObjectsFromArray:[NSArray sortArray:arrayM]];
//    [_tableView reloadData];
//}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if(tableView == _tableView)
    {
        NSInteger rows = 0;
        switch (section) {
            case 0:
                rows = 4;
                break;
            default:
                rows = 0;
                break;
        }
        return rows;
    }
    else
    {
        NSArray *array = _searchSectionListArray[section];
        return array.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    if(tableView == _tableView)
    {
        return 1;
    }
    else
    {
        return _searchSectionListArray.count;
    }
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    if(tableView == _tableView)
    {
        static NSString *NormalCellIdentifier = @"SettingNormalCell";
        SettingNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
        if (normalCell == nil) {
            normalCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingNormalCell" owner:self options:nil] objectAtIndex:0];
        }
        if (indexPath.section == 0) {
            [normalCell.icon setImage:[UIImage imageNamed:[self iconName:indexPath]] forState:UIControlStateNormal];
            normalCell.rightArrow.hidden = NO;
            [normalCell.titleLabel setText:[self titleName:indexPath]];
            normalCell.icon.layer.cornerRadius = 5.0f/2;
            normalCell.icon.layer.masksToBounds = YES;
            if (indexPath.row == 1) {
                normalCell.seperatorLine.hidden = YES;
            }
            return normalCell;

        } else {
            CContactModel *contact = self.contactList[indexPath.row];
            static NSString *NormalCellIdentifier = @"GroupMenberCell";
            GroupMenberCell *groupMemberCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
            if (groupMemberCell == nil) {
                groupMemberCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
            }
            [groupMemberCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contact.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
            //        [groupMemberCell.titleLabel setText:@"测试名字"];
            [groupMemberCell.titleLabel setText:contact.UserName];
            NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];
            if(indexPath.row == rows - 1)
            {
                [groupMemberCell.seperatorLine setHidden:YES];
            } else {
                groupMemberCell.seperatorLine.hidden = NO;
            }
            return groupMemberCell;
        }
    }
    else
    {
        NSArray *searchArray = _searchSectionListArray[indexPath.section];
        id lastContact = searchArray[indexPath.row];
        if ([lastContact isKindOfClass:[CGroupModel class]]) {
            CGroupModel *groupModel = (CGroupModel *)lastContact;
            
            static NSString *NormalCellIdentifier = @"SettingNormalCell";
            SettingNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
            if (normalCell == nil) {
                normalCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingNormalCell" owner:self options:nil] objectAtIndex:0];
            }
            if (groupModel.groupType == 1) {
                [normalCell.icon setImage:[UIImage imageNamed:@"icon-discussList"] forState:UIControlStateNormal];
            } else {
                [normalCell.icon setImage:[UIImage imageNamed:@"icon-Group"] forState:UIControlStateNormal];
            }
            
            normalCell.icon.layer.cornerRadius = 5.0f/2;
            normalCell.icon.layer.masksToBounds = YES;
            [normalCell.titleLabel setText:[NSString stringWithFormat:@"%@(%zd)",groupModel.GroupName,groupModel.UserNum]];
            normalCell.rightArrow.hidden = YES;
            NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];

            if(indexPath.row == rows - 1){
                
                [normalCell.seperatorLine setHidden:YES];
            }
            else{
                [normalCell.seperatorLine setHidden:NO];
            }
            return normalCell;
            
        } else {
            CContactModel *contact = (CContactModel *)lastContact;
            static NSString *GroupCellIdentifier = @"GroupMenberCell";
            GroupMenberCell *groupMemberCell = [tableView dequeueReusableCellWithIdentifier:GroupCellIdentifier];
            if (groupMemberCell == nil) {
                groupMemberCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
            }
            groupMemberCell.Arrow.hidden = YES;
            [groupMemberCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contact.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
            //        [groupMemberCell.titleLabel setText:@"测试名字"];
            [groupMemberCell.titleLabel setText:contact.UserName];
            NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];
           
            if(indexPath.row == rows - 1)
            {
                [groupMemberCell.seperatorLine setHidden:YES];
            } else {
                groupMemberCell.seperatorLine.hidden = NO;
            }
            return groupMemberCell;
            
        }
    }
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"组织架构";
                    break;
                case 1:
                    title = @"群";
                    break;
                case 2:
                    title = @"讨论组";
                    break;
                case 3:
                    title = @"我的联系人";
                    break;

                default:
                    break;
            }
        }
            break;
        case 1:
        {
            title = @"孙思明";
        }
            break;
        default:
            break;
    }
    return title;
}

-(NSString *)iconName:(NSIndexPath *)indexPath
{
    NSString *iconName = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    iconName = @"icon-organization";
                    break;
                case 1:
                    iconName = @"icon-group1";
                    break;
                case 2:
                    iconName = @"icon-comment";
                    break;
                case 3:
                    iconName = @"icon-mineContact";
                    break;

                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return iconName;
}



#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (_tableView == tableView) {
        
      
        return 0.01f;
    } else {
        return KSECTIONHEIGHT;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0.01f;
    switch (section) {
        case 1:
            height = 0.01f;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
    if (_tableView == tableView) {
        if (section == 1) {
            headerView = [[UIView alloc] init];
            headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
            headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
            UIView *beginLine = [[UIView alloc] init];
            beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
            [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
            [headerView addSubview:beginLine];
            
            UIView *endLine = [[UIView alloc] init];
            endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
            [endLine setBackgroundColor:KSEPERATORLINECOLOR];
            [headerView addSubview:endLine];
            
            UILabel *label = [[UILabel alloc] init];
            
            [label setText:@"常用联系人"];
            [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
            label.frame = CGRectMake(15, 1, 200, KSECTIONHEIGHT-1);
            [headerView addSubview:label];

        }
    } else {
        
        headerView = [[UIView alloc] init];
        headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
        headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
        UIView *beginLine = [[UIView alloc] init];
        beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
        [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
        [headerView addSubview:beginLine];
        
        UIView *endLine = [[UIView alloc] init];
        endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
        [endLine setBackgroundColor:KSEPERATORLINECOLOR];
        [headerView addSubview:endLine];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = _searchAllFirstLetter[section];
        [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
        label.frame = CGRectMake(15, 1, 200, KSECTIONHEIGHT-1);
        [headerView addSubview:label];

        
    }
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
//    if(section != 1)
//    {
//        return headerView;
//    }
//    headerView = [[UIView alloc] init];
//    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
//    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//    UIView *beginLine = [[UIView alloc] init];
//    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//    [headerView addSubview:beginLine];
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        CGFloat height = 60.0f;
        
        if(indexPath.row == 0 && indexPath.section == 0)
        {
            height = 60.0f;
        }
        return height;
        
    } else {
        return 60.0f;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == _tableView) {
        if (_tableView == tableView) {
            switch (indexPath.section) {
                case 0:
                {
                    switch (indexPath.row) {
                        case 0:
                        {
                            [self showDepartDetailVC];
                        }
                            break;
                        case 1:
                        {
                            [self showGroupListVC];
                        }
                            break;
                        case 2:
                        {
                            [self showDiscussVc];
                        }
                            break;
                        case 3:
                        {
                            [self showMineContactVC];
                        }
                            break;
                            
                        default:
                            break;
                    }
                }
                    break;
                    
                    
                case 1:
                {
                    CContactModel *member = [self.contactList objectAtIndex:indexPath.row];
                    CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:member.DEPTID];
                    [self showMembersDetailVCWithMember:member deptName:dep.DepartName];
                }
                    break;
                    
                default:
                    break;
            }
            
        }
        
    } else {
        NSArray *rowArray = _searchSectionListArray[indexPath.section];
        id model = rowArray[indexPath.row];
        if ([model isKindOfClass:[CContactModel class]]) {
            CContactModel *contact = (CContactModel *)model;
            CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
            
            [self showMembersDetailVCWithMember:contact deptName:dep.DepartName];
        } else if ([model isKindOfClass:[CGroupModel class]]) {
            CGroupModel *groupModel = (CGroupModel *)model;
            if (groupModel) {
                CHChatRoomBase *chat = [[CHChatRoomBase alloc] init];
                chat.group = groupModel;
                chat.member = nil;
                UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                nav.navigationBarHidden = NO;
                self.navigationController.navigationBarHidden = YES;
                
                [nav pushViewController:chat animated:YES];
            } else {
                
            }

            
        }


    }
    
    
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_tableView == tableView) {
        return nil;
    } else{
        return _searchAllFirstLetter;
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSInteger count = 0;
    if(tableView == _tableView)
    {
        count = index;
    } else {
        count = index;
    }
    return count;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1)
    {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            
            CContactModel *contact = self.contactList[indexPath.row];
            CRecentlyContactMgr *reMgr = [[CRecentlyContactMgr alloc] init];
            [reMgr delRecentlyContact:contact.UserID];
            [[CXmppRequestMgr shareInstance] delOftenContactWithUserId:contact.UserID];
            [self.contactList removeObjectAtIndex:indexPath.row];
            [_tableView reloadData];
    }
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
        
    } else {
        
        
    }

}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_tableView == tableView) {
        if(indexPath.section == 1)
        {
            return YES;
        }
        return NO;
        
    } else {
        return NO;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
{
    return @"删除";
}



- (void)showDiscussVc
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    EFengDiscussController *discuss = [[EFengDiscussController alloc] init];
    [nav pushViewController:discuss animated:YES];
}

- (void)showMineContactVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    EFengMIneContactController *mineContact = [[EFengMIneContactController alloc] init];
    [nav pushViewController:mineContact animated:YES];
}
-(void)showDepartDetailVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    NSArray *deps = [[[CDepartmentMgr alloc] init] loadDepartments:@"0"];
    CDepartmentModel *dep = deps.lastObject;
    DepartDetailVC *departDetail = [[DepartDetailVC alloc] initWithDepartId:dep.DEPTID];
//    CHSNavigationController *departDetailNav = [[CHSNavigationController alloc] initWithRootViewController:departDetail];
//    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
    [nav pushViewController:departDetail animated:YES];
    
}

-(void)showGroupListVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    GroupListVC *groupList = [[GroupListVC alloc] init];
//    CHSNavigationController *departDetailNav = [[CHSNavigationController alloc] initWithRootViewController:groupList];
    //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
    [nav pushViewController:groupList animated:YES];
    
    
}


//MyInformationVC
-(void)showMembersDetailVCWithMember:(CContactModel *)model deptName:(NSString *)deptName
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    //    CHSNavigationController *groupMemberDetailNav = [[CHSNavigationController alloc] initWithRootViewController:groupMemberDetail];
    if ([[[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId isEqualToString:model.UserID]) {
        
        MyInformationVC *myInfo = [[MyInformationVC alloc] init];
        //    CHSNavigationController *myInfoNav = [[CHSNavigationController alloc] initWithRootViewController:myInfo];
        [nav pushViewController:myInfo animated:YES];
        //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
        
    }else {
    //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
        GroupMemberDetailVC *groupMemberDetail = [[GroupMemberDetailVC alloc] init];
        groupMemberDetail.member = model;
        groupMemberDetail.deptName = deptName;

    [nav pushViewController:groupMemberDetail animated:YES];
    
    }

}

//-(void)showMembersDetailVC
//{
//    GroupMemberDetailVC *groupMemberDetail = [[GroupMemberDetailVC alloc] init];
//    CHSNavigationController *groupMemberDetailNav = [[CHSNavigationController alloc] initWithRootViewController:groupMemberDetail];
//    //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
//    [self.navigationController pushViewController:groupMemberDetail animated:YES];
//    [self.leveyTabBarController hidesTabBar:YES animated:NO];
//}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [_searchSectionListArray removeAllObjects];
    [_searchAllFirstLetter removeAllObjects];
    NSArray *contactArray = [[[CContactMgr alloc] init] searchContactWithSeachStr:searchText];
    NSArray *groupArray = [[[CGroupMgr alloc] init] searchGroupWithSeachStr:searchText withGroupType:2];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for (CGroupModel *discuss in groupArray) {
        if ([discuss.OwnerId isEqualToString:self.curContact.UserID]) {
            [arrayM addObject:discuss];
        } else {
            if (discuss.isShow || discuss.groupType == 0) {
                [arrayM addObject:discuss];
            }
        }
    }

    
    NSMutableArray *searchArray = [NSMutableArray array];
    [searchArray addObjectsFromArray:contactArray];
    [searchArray addObjectsFromArray:groupArray];
    _searchAllFirstLetter = [NSArray IndexArray:searchArray];
    _searchSectionListArray = [NSArray LetterSortArray:searchArray];
    [_tableView reloadData];
}
//bug 8510 fixed by lixu
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0){
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{ for(UIView *subview in controller.searchResultsTableView.subviews) {
    
    if([subview isKindOfClass:[UILabel class]]) {
        
        [(UILabel*)subview setText:@"未找到匹配的信息"];
        
    }
    
}
    return YES;
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar                     // called when text starts editing
{
    searchBar.showsCancelButton = YES;
    for(id cc in [_searchDisplayController.searchBar subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }

}



#pragma mark-- UISearchDisplayDelegate
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)
{
    UIView *topView = controller.searchBar.subviews[0];
    
    for (UIView *subView in topView.subviews) {
        
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            
            UIButton * cancelButton = (UIButton*)subView;
            
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            
        }
        
    }
}

@end
